﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using IBatisNet.DataMapper;
using EIA.PM.DAL.IBatisMapping;

namespace EIA.PM.DAL.IBatis
{
    public class Dao<TModel> : IDao<TModel>
    {
        protected ISqlMapper _sqlMapper = null;

        public Dao()
        {
            _sqlMapper = SqlMapperHelper.GetSqlMapper();
        }

        #region IDao<TModel>

        public virtual object Insert(TModel model)
        {
            string statement = string.Format("Insert{0}", typeof(TModel).Name);
            object key = _sqlMapper.Insert(statement, model);
            return key;
        }

        public object Insert(IDictionary props)
        {
            string statement = string.Format("Insert{0}", typeof(TModel).Name);
            object key = _sqlMapper.Insert(statement, props);
            return key;
        }

        public TModel Get(object id)
        {
            string statement = string.Format("Select{0}", typeof(TModel).Name);
            TModel model = _sqlMapper.QueryForObject<TModel>(statement, id);
            return model;
        }

        public IList<TModel> List(IDictionary props)
        {
            string statement = string.Format("Select{0}_CV", typeof(TModel).Name);
            IList<TModel> models = _sqlMapper.QueryForList<TModel>(statement, props);
            return models;
        }

        public int Count(IDictionary props)
        {
            string statement = string.Format("Select{0}_PC", typeof(TModel).Name);
            int count = _sqlMapper.QueryForObject<int>(statement, props);
            return count;
        }

        public IList<TModel> List(IDictionary props, int pagesize, int page)
        {
            int offset = pagesize * (page - 1);
            int count = Count(props);
            int rows = offset + pagesize - count > 0 ? count - offset : pagesize;
            string statement = string.Format("Select{0}_PG", typeof(TModel).Name);
            if (props == null)
            {
                props = new Dictionary<string, object>();
            }
            props["_offset"] = offset;
            props["_rows"] = rows;
            IList<TModel> models = _sqlMapper.QueryForList<TModel>(statement, props);
            props.Remove("_offset");
            props.Remove("_rows");
            return models;
        }

        public virtual bool Update(TModel model)
        {
            string statement = string.Format("Update{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Update(statement, model);
            return effect > 0;
        }

        public virtual bool Update(IDictionary props)
        {
            string statement = string.Format("Update{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Update(statement, props);
            return effect > 0;
        }

        public int Delete(object id)
        {
            string statement = string.Format("Delete{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, id);
            return effect;
        }

        public int Delete(TModel model)
        {
            string statement = string.Format("Delete{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, model);
            return effect;
        }

        public int Delete(IDictionary props)
        {
            string statement = string.Format("Delete{0}_CV", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, props);
            return effect;
        }

        public virtual int Clear()
        {
            string statement = string.Format("Clear{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, null);
            return effect;
        }

        private IDASession _daSession = null;
        public virtual IDASession DASession
        {
            get
            {
                if (null == _daSession) { _daSession = new DASession(_sqlMapper); }
                return _daSession;
            }
        }

        #endregion

        /// <summary>
        /// 得到运行时ibatis.net动态生成的SQL
        /// </summary>
        /// <param name="sqlMapper"></param>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public static string GetRuntimeSql(ISqlMapper sqlMapper, string statementName, object paramObject)
        {
            string result = string.Empty;
            try
            {
                IBatisNet.DataMapper.MappedStatements.IMappedStatement statement = sqlMapper.GetMappedStatement(statementName);
                if (!sqlMapper.IsSessionStarted)
                {
                    sqlMapper.OpenConnection();
                }
                IBatisNet.DataMapper.Scope.RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, sqlMapper.LocalSession);
                result = scope.PreparedStatement.PreparedSql;
            }
            catch (Exception ex)
            {
                result = "获取SQL语句出现异常:" + ex.Message;
            }
            return result;
        }
    }
}
